package com.css.dzj.dzjportal.modules.supervision.service.impl;

import com.css.dzj.dzjportal.common.util.Constant;
import com.css.dzj.dzjportal.modules.supervision.entity.QSupervisionMatterEntity;
import com.css.dzj.dzjportal.modules.supervision.entity.SupervisionMatterEntity;
import com.css.dzj.dzjportal.modules.supervision.mapper.SupervisionMapper;
import com.css.dzj.dzjportal.modules.supervision.service.SupervisionService;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;


/**
 * 监管事项清单service实现类
 */
@Service
public class SupervisionServiceImpl implements SupervisionService {

    @Autowired
    private SupervisionMapper supervisionMapper;

    @Autowired
    private JPAQueryFactory jpaQueryFactory;

    /**
     * 添加监管事项清单
     * @param supervisionMatterEntity
     */
    @Override
    @Transactional
    public void add(SupervisionMatterEntity supervisionMatterEntity) {
        supervisionMapper.save(supervisionMatterEntity);
    }


    /**
     * 通过id获取监管事项清单
     * @param id
     * @return
     */
    @Override
    public SupervisionMatterEntity queryById(String id) {
        return supervisionMapper.findById(id).get();
    }


    /**
     * 修改监管事项清单
     * @param supervisionMatterEntity
     */
    @Override
    @Transactional
    public void update(SupervisionMatterEntity supervisionMatterEntity) {

    }


    /**
     * 多条件分页查询监管事项清单
     * @param supervisionNumber
     * @param supervisionName
     * @param supervisionObject
     * @param permitType
     * @param supervisionForm
     * @param isPublic
     * @param publicLocation
     * @param status
     * @param pageable
     * @return
     */
    @Override
    public QueryResults<SupervisionMatterEntity> listPage(String supervisionNumber, String supervisionName, String supervisionObject, String permitType, String supervisionForm, String isPublic, String publicLocation, String status, Pageable pageable) {
        QSupervisionMatterEntity qEntity = QSupervisionMatterEntity.supervisionMatterEntity;
        Predicate predicate = qEntity.isNotNull().or(qEntity.isNull());
        predicate = (supervisionNumber == null || supervisionNumber.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qEntity.supervisionNumber.like("%" + supervisionNumber +"%"));
        predicate = (supervisionName == null || supervisionName.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qEntity.supervisionName.like("%" + supervisionName + "%"));
        predicate = (supervisionObject == null || supervisionObject.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qEntity.supervisionObject.like("%" + supervisionObject + "%"));
        predicate = (permitType == null || permitType.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qEntity.permitType.eq(permitType));
        predicate = (supervisionForm == null || supervisionForm.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qEntity.supervisionForm.eq(supervisionForm));
        predicate = (isPublic == null || isPublic.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qEntity.isPublic.eq(isPublic));
        predicate = (publicLocation == null || publicLocation.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qEntity.publicLocation.eq(publicLocation));
        predicate = (status == null || status.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qEntity.status.eq(status));
        predicate = ExpressionUtils.and(predicate, qEntity.delFlag.eq(Constant.NO_DELETE));
        return jpaQueryFactory.selectFrom(qEntity)
                .where(predicate)
                .orderBy(qEntity.createTime.desc())
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchResults();
    }


    /**
     * 分页查询所有监管事项清单
     * @param pageable
     * @return
     */
    @Override
    public QueryResults<SupervisionMatterEntity> getPageAll(Pageable pageable) {
        QSupervisionMatterEntity qEntity = QSupervisionMatterEntity.supervisionMatterEntity;
        Predicate predicate = qEntity.isNotNull().or(qEntity.isNull());
        predicate = ExpressionUtils.and(predicate, qEntity.delFlag.eq(Constant.NO_DELETE));
        return jpaQueryFactory.selectFrom(qEntity)
                .where(predicate)
                .orderBy(qEntity.createTime.desc())
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchResults();
    }

    /**
     * 分页查询所有已公开的监管事项清单
     * @param supervisionName
     * @param publicLocation
     * @param pageable
     * @return
     */
    @Override
    public QueryResults<SupervisionMatterEntity> queryPublic(String supervisionName, String publicLocation, Pageable pageable) {
        QSupervisionMatterEntity q = QSupervisionMatterEntity.supervisionMatterEntity;
        Predicate predicate = q.isNotNull().or(q.isNull());
        predicate = (supervisionName == null || supervisionName.isEmpty()) ? predicate : ExpressionUtils.and(predicate, q.supervisionName.like("%" + supervisionName + "%"));
        predicate = (publicLocation == null || publicLocation.isEmpty()) ? predicate : ExpressionUtils.and(predicate, q.publicLocation.like("%" + publicLocation + "%"));
        predicate = ExpressionUtils.and(predicate, q.status.eq("1"));
        return jpaQueryFactory.selectFrom(q)
                .where(predicate)
                .orderBy(q.createTime.desc())
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchResults();
    }


}
